var index = {
    color: ['rgb(68,114,196)', 'rgb(165,165,165)', 'rgb(237,125,49)', 'rgb(255,192,0)',
        'rgb(153,216,123)', "rgb(0,174,157)", 'rgb(101,194,148)', 'rgb(199,126,181)',
        'rgb(0,154,214)', 'rgb(175,180,219)', 'rgb(163,207,98)', 'rgb(37,133,166)', 'rgb(105,77,159)'],
    county: ['全市', '云岩区', '南明区', '观山湖区', '花溪区', '乌当区', '白云区', '清镇市', '修文县', '开阳县', '息烽县', '贵安新区'],
    compareTableHead: ['序号', '对比项目', '对比分值项'],//比对数据表头
    compareItemTableHead: ['问卷项目', '贵阳监测（百分比）', '教育部十二城市监测（百分比）', '教育部十二城市监测（百分比）'],//指标项目比对表头
    isShowSmallDataLabel: false,//是否现实数据标签
    isShowDataLabel: false,//是否显示echarts数据标签
    isExport: true,//是否可以导出
    isShowDataTable: true,//是否显示数据表格
    toolBoxShow: false,
    //getSeries
    getSeries: function (data) {
        var yearArr = ['2018', '2019', '2020', '2021'];
        var series = [];
        for (var i = 0; i < yearArr.length; i++) {
            var serie = {
                name: yearArr[i],
                type: 'bar',
                data: data[yearArr[i]],
                label: {
                    normal: {
                        show: index.isShowSmallDataLabel,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        }
                    }
                }
            };
            series.push(serie);
        }
        return series;

    },
    //echarts画图工具
    drawChart: function (txtCla, txt, divId, data, dataDetail) {
        $(txtCla).text(data[txt]);//文本赋值
        var myChart = echarts.init(document.getElementById(divId));
        var option = {
            animation: false,
            color: index.color,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: Object.keys(data[dataDetail]),
                top: 'bottom'
            },
            toolbox: {
                show: index.toolBoxShow,
                orient: 'vertical',
                right: 'right',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            xAxis: [
                {
                    "axisLabel": {     //加上这个强制显示
                        interval: 0
                    },
                    type: 'category',
                    axisTick: {show: false},
                    data: index.county
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: index.getSeries(data[dataDetail])
        };
        myChart.setOption(option);
    },
    /**
     *@description 创建数据表格
     *@author XieXing
     * @param data 数据
     * @param xAxis 横坐标
     * @param tableClass 对应表格class
     *@date 2021/12/29 16:21
     */
    drawTable: function (data, xAxis, tableClass) {
        if (!index.isShowDataTable) {
            return false;
        }
        var str = "<tr><th>年份</th>";
        for (var i = 0; i < xAxis.length; i++) {
            str += ("<th>" + xAxis[i] + "</th>");
        }
        str += "</tr>";
        for (var obj in data) {
            str += "<tr><td>" + obj + "</td>";
            for (var i = 0; i < data[obj].length; i++) {
                var t = index.invalidDataTest(data[obj][i]);
                str += ("<td>" + t + "</td>");
            }
            str += "</tr>";
        }
        $(tableClass).html(str);
    },
    //无效数据监测
    invalidDataTest: function (data) {
        if (!data || data == '0.00') {
            return "-";
        }
        return data
    },
    //总体监测结果
    chart1: function (data) {
        index.drawChart(".txt1", "allSituationTxt", "allSituation", data, "allSituationScore");
        index.drawTable(data['allSituationScore'], index.county, ".allSituationTable");
    },
    //五年级达成度总体监测结果
    chart2: function (data) {
        index.drawChart(".txt2", "primaryDcdAllSituationTxt", "primaryDcdAllSituation", data, "primaryDcdAllSituationScore");
        index.drawTable(data['primaryDcdAllSituationScore'], index.county, ".primaryDcdAllSituationTable");
    },
    //描绘检测内容
    drawTestContent: function (data, xd) {
        var testContent = data[xd + 'TestContent'];
        for (var i = 0; i < testContent.length; i++) {
            var obj = testContent[i];
            var str = ' <p class="text-indent line-h-28">\n' +
                '                            <label>' + obj["index"] + '</label><span>' + obj["content"] + '</span></p>\n' +
                '                        <div id="' + xd + 'TestContent' + i + '" class="chart">\n' +
                '\n' +
                '                        </div>\n' +
                '                        <table class="' + xd + 'TestContentTable' + i + ' table-bordered">\n' +
                '\n' +
                '                        </table>\n' +
                '                        <p class="text-indent line-h-28"><span>' + obj["detail"] + '</span></p>\n' +
                '                    ';
            $('.' + xd + '-test-content').append(str);
            index.drawChart('', '', xd + 'TestContent' + i, obj, 'score');
            index.drawTable(obj['score'], index.county, '.' + xd + 'TestContentTable' + i);
        }
    },

    //数据比对描述
    drawCompareDataTxt: function (data, xd) {
        var str = "";
        for (var i=0;i<data.length;i++) {
            str += "<br>"+data[i]['index'] + ":" + data[i]['diffTxt'];
        }
        $("." + xd + "-compareDataTxt").html(str);
    },
    //描绘比对表格
    drawCompareTable: function (data, xd) {
        var str = "<tr>";
        var head = index.compareTableHead.concat(index.county);
        for (var i = 0; i < head.length; i++) {
            str += "<th>" + head[i] + "</th>";
        }
        str += "</tr>";
        for (var i = 0; i < data.length; i++) {
            var obj = data[i];
            var preYearData = obj['2020'];
            str += "<tr><td rowspan='3'>" + (i + 1) + "</td><td rowspan='3'>" + obj['index'] + "</td><td>2020年</td>";
            for (var t = 0; t < preYearData.length; t++) {
                var tempData = preYearData[t] ? preYearData[t] : "-";
                str += "<td>" + tempData + "</td>";
            }
            str += "</tr>";
            var curYearData = obj['2021'];
            str += "<tr><td>2021年</td>";
            for (var t = 0; t < curYearData.length; t++) {
                str += "<td>" + curYearData[t] + "</td>";
            }
            str += "</tr>";
            var compareData = obj['diff'];
            str += "<tr><td>波动值</td>";
            for (var t = 0; t < compareData.length; t++) {
                var tempData = compareData[t] ? compareData[t] : "-";
                if (tempData < 0) {
                    str += "<td class='red'>" + tempData + "</td>";
                } else if (tempData >= 0) {
                    str += "<td class='blue'>" + tempData + "</td>";
                } else {
                    str += "<td>" + tempData + "</td>";
                }
            }
            str += "</tr>";
        }
        $("." + xd + "CompareTable").html(str);
    },

    //描绘指标对比
    drawItemTable: function (data, xd) {
        var str = "<tr>";
        for (var i = 0; i < index.compareItemTableHead.length; i++) {
            str += "<th>" + index.compareItemTableHead[i] + "</th>";
        }
        str += "</tr>";
        for (var i = 0; i < data.length; i++) {
            var obj = data[i];
            str += "<tr><td>" + obj['item'] + "</td><td>" + obj['gy'] + "</td><td>" + obj['edu'] + "</td><td>" + obj['coastalCity'] + "</td></tr>";
        }
        $("." + xd + "ItemTable").html(str);
    },

    //相关性描绘
    drawRelevant: function (data, xd) {
        $("." + xd + "RelevantSummary").text(data[xd + "Relevant"]['summary']);
        var items = data[xd + "Relevant"]['item'];
        var str = "";
        for (var i = 0; i < items.length; i++) {
            str += '<p class="text-indent line-h-28">（' + (i + 1) + ')' + items[i] + '</p>';
        }
        $("." + xd + "RelevantItem").html(str);
    },

    //八年级达成度总体监测结果
    chart3: function (data) {
        index.drawChart(".txt3", "juniorDcdAllSituationTxt", "juniorDcdAllSituation", data, "juniorDcdAllSituationScore");
        index.drawTable(data['juniorDcdAllSituationScore'], index.county, ".juniorDcdAllSituationTable");
    },
    //高二达成度总体监测结果
    chart4: function (data) {
        index.drawChart(".txt4", "seniorDcdAllSituationTxt", "seniorDcdAllSituation", data, "seniorDcdAllSituationScore");
        index.drawTable(data['seniorDcdAllSituationScore'], index.county, ".seniorDcdAllSituationTable");
    },

    canvas2Img: function (claObject) {
        //将第一个画布作为基准。
        var baseCanvas = $(claObject).find("canvas").first()[0];
        if (!baseCanvas) {
            return false;
        }
        var width = baseCanvas.width;
        var height = baseCanvas.height;
        var ctx = baseCanvas.getContext("2d");
        //遍历，将后续的画布添加到在第一个上
        $(claObject).find("canvas").each(function (i, canvasObj) {
            if (i > 0) {
                var canvasTmp = $(canvasObj)[0];
                ctx.drawImage(canvasTmp, 0, 0, width, height);
            }
        });
        //获取base64位的url
        var base64Str = baseCanvas.toDataURL();
        $(claObject).html("<img src=" + base64Str + " alt='图片不存在'> ");
        return baseCanvas.toDataURL();
    },
    init2: function () {
        if (!index.isExport) {
            return false;
        }
        var echarts = $('.chart');
        for (var i = 0; i < echarts.length; i++) {
            index.canvas2Img(echarts[i]);
        }
    },
    init: function () {
        // $.get("/dyReportData/getData", {}, function (data) {
        $.get("/static/json/moral_education/city_report.json", {}, function (data) {
            $(".studentNumber").text(data['studentNumber']);
            $(".primaryStudentNumber").text(data['primaryStudentNumber']);
            $(".juniorStudentNumber").text(data['juniorStudentNumber']);
            $(".seniorStudentNumber").text(data['seniorStudentNumber']);
            index.chart1(data);
            index.chart2(data);
            index.chart3(data);
            index.chart4(data);
            //五年级
            index.drawTestContent(data, 'primary');
            index.drawCompareDataTxt(data['primaryCompareTxt'],'primary');
            index.drawCompareTable(data['primaryCompareData'], 'primary');
            // index.drawItemTable(data['primaryItemCompare'], 'primary');
            // index.drawRelevant(data, 'primary');
            //八年级
            index.drawTestContent(data, 'junior');
            index.drawCompareDataTxt(data['juniorCompareTxt'],'junior');
            index.drawCompareTable(data['juniorCompareData'], 'junior');
            // index.drawItemTable(data['juniorItemCompare'], 'junior');
            // index.drawRelevant(data, 'junior');
            //高二
            index.drawTestContent(data, 'senior');
            index.drawCompareDataTxt(data['seniorCompareTxt'],'senior');
            index.drawCompareTable(data['seniorCompareData'], 'senior');
            // index.drawItemTable(data['seniorItemCompare'], 'senior');
            // index.drawRelevant(data, 'senior');
            index.init2();
        });
    }
};